<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsCheckoutBean {
	
	public function __construct() {
	// contructor function
	}
	
	 /*	get all email receiver 
	 */
	public static function get_all_email_receiver($db) {
		
    	$strSQL = sprintf("SELECT * FROM ".TBL_EMAIL_RECEIVER." e ".
						  "LEFT JOIN ".TBL_USER." u ON u.user_id=e.user_id");
    	return $db->get_results($strSQL);
    }
	
	/*returns the whole active left sided country table
	 */ 
	public static function get_all_shipping_method($db) {
		
    	$strSQL = sprintf("SELECT * FROM ".TBL_SHIPPINGMETHOD);
    	return $db->get_results($strSQL);
    }
	
	public static function get_shipping_method_by_code($db,$code) {
		
    	$strSQL = sprintf("SELECT * FROM ".TBL_SHIPPINGMETHOD." WHERE code='$code'");
    	return $db->get_row($strSQL);
    }
	
	public static function save_order_list($db, $order,$orderproduct)
	{
		$strSQL = sprintf(
						"INSERT INTO %s(customer_id,".
						"delivery_gender, delivery_firstname, delivery_lastname, ".
						"delivery_company, delivery_street_address, delivery_city, ".
						"delivery_postcode, delivery_state, delivery_country, delivery_telephone, delivery_email_address, ".
						"billing_gender, billing_firstname, billing_lastname, ".
						"billing_company, billing_street_address, billing_city, ".
						"billing_postcode, billing_state, billing_country, billing_telephone, billing_email_address, ".
						"payment_method, shipping_method,delivery_cost, currency, currency_value, notes, orders_date_finished)".
						"VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', ".
						"'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', now())",
						TBL_ORDER_INFO,
						$order['customer_id'],
						$order['delivery_gender'],
						$order['delivery_firstname'],
						$order['delivery_lastname'],
						$order['delivery_company'],
						$order['delivery_street_address'],
						$order['delivery_city'],
						$order['delivery_postcode'],
						$order['delivery_state'],
						$order['delivery_country'],
						$order['delivery_telephone'],
						$order['delivery_email_address'],
						$order['billing_gender'],
						$order['billing_firstname'],
						$order['billing_lastname'],
						$order['billing_company'],
						$order['billing_street_address'],
						$order['billing_city'],
						$order['billing_postcode'],
						$order['billing_state'],
						$order['billing_country'],
						$order['billing_telephone'],
						$order['billing_email_address'],
						$order['payment_method'],
						$order['shipping_method'],
						$order['delivery_cost'],
						$order['currency'],
						$order['currency_value'],
						$order['notes']);
		$res=$db->query($strSQL);
		$orderid = $db->insert_id;
		foreach($orderproduct as $item){
			FgcmsCheckoutBean::save_order_product($db,$orderid,$item);
		}
		
		if($res == true)
    		return $orderid;
    	else
    		return -1;
		
	}
	
	public static function save_order_product($db,$so_id, $order)
	{
			$strSQL = sprintf(
						"INSERT INTO %s(so_id,product_id,product_name,product_price,final_price,product_tax,product_quantity)".
						"VALUES('%s','%s','%s','%s','%s','%s','%s')",
						TBL_ORDER_PRODUCT,
						$so_id,
						$order->product_id,
						$order->product_name,
						$order->product_price,
						$order->total,
						$order->tax,
						$order->quantity);
			$res=$db->query($strSQL);			
	}
	
	public static function update_pay_success($db, $id){
    	
    	$sql = sprintf("UPDATE %s SET " .
    			"pay_status = '1' ," .
				"date_pay = now() " .
    			"WHERE so_id = '$id' " ,
    			TBL_ORDER_INFO);
    	
    	$res = $db->query($sql);

    	return $res;
    }
	
	public static function getAllPaymentMethod($db) {
		$strSQL = " SELECT * FROM ".TBL_PAYMENT_METHOD;
		$res = $db->get_results($strSQL);
		return $res;
	}
	
	public static function getConfigKey($db,$key) {
		$strSQL = " SELECT * FROM ".TBL_PAYMENT_CONFIG." WHERE config_key = '$key'";
		$res = $db->get_row($strSQL);
		return $res->value;
	}
	
	public static function get_incoming_order($db,$id) {
		$sql = "SELECT * FROM " . TBL_ORDER_INFO." WHERE customer_id = $id ORDER BY so_id DESC LIMIT 0,1";
		$res = $db->get_row($sql);
    	
    	return $res;
	}
}
?>
